package generate;

/* @author mdm32
 */
public class LineSegment {
    private String lName;
    private double X1;
    private double Y1;
    private double X2;
    private double Y2;

    //constructors
    public LineSegment(){
        lName = "0";
        X1 = 0;
        Y1 = 0;
        X2 = 1;
        Y2 = 1;
    }

    public LineSegment(String name, double x1, double y1,
            double x2, double y2){
        lName = name;
        X1 = x1;
        Y1 = y1;
        X2 = x2;
        Y2 = y2;
    }

    //recover data
    public String getName(){
        return lName;
    }
    public double getX1(){
        return X1;
    }
    public double getX2(){
        return X2;
    }
    public double getY1(){
        return Y1;
    }
    public double getY2(){
        return Y2;
    }

    //set data
    public void setName(String s){
        lName = s;
    }
    public void setX1(double x1){
        X1 = x1;
    }
    public void setX2(double x2){
        X2 = x2;
    }
    public void setY1(double y1){
        Y1 = y1;
    }
    public void setY2(double y2){
        Y2 = y2;
    }

    //additional methods
    public double segmentLength(){
    	  double cy = Y2-Y1;
          double cx = X2-X1;
          return Math.sqrt(cx*cx+cy*cy);
      }
      public void printSegment(){
          System.out.println(lName + " (" + X1 + "," + Y1 +
                  "), (" + X2 + "," + Y2 + ")");
      }
      
      //the following methods are by Andrew Leach
      
      //returns the line equation type for the line segment
      //boolean true if y = mx +b, false if x = b
      public boolean isNotVertical(){
    	  if(X1==X2){
    		  return false;
    	  }
    	  return true;
      }
      
      //returns the Y value of the line segment at a given X value
      public double yValue(double x){
    	  if(isNotVertical()){
    		  return (Y2-Y1)*x/(X2-X1)+Y1-(X1*(Y2-Y1)/(X2-X1));
    	  }
    	  else{
    		  return 0;
    	  }
      }
      
      //returns slope of the linesegment
      public double slope(){
    	  if(isNotVertical()){
    		  return (Y2-Y1)/(X2-X1);
    	  }
    	  else{
    		  //beware this is actually a vertical line,
    		  //not horizontal, but cannot return infinity
    		  return 0;
    	  }
      }
  }